package com.lyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyun.mapper.SettingsMapper;
import com.lyun.pojo.Settings;
import com.lyun.service.SettingService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@DubboService
public class SettingServiceImpl extends ServiceImpl<SettingsMapper, Settings> implements SettingService {

    @Resource
    private SettingsMapper settingsMapper;

    @Override
    public Settings getByPhone(String phone) {
        LambdaQueryWrapper<Settings> queryWrappe = new LambdaQueryWrapper<>();
        queryWrappe.eq(Settings::getPhone, phone);
        return settingsMapper.selectOne(queryWrappe);
    }

    @Override
    public boolean updateQuestions(Settings settings) {
        LambdaQueryWrapper<Settings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Settings::getUserId, settings.getUserId());
        return settingsMapper.update(settings, queryWrapper) > 0;
    }

    @Override
    public boolean changeSettings(Settings settings) {
        LambdaQueryWrapper<Settings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Settings::getPhone, settings.getPhone());
        return settingsMapper.update(settings, queryWrapper) > 0;
    }

    @Override
    public boolean saveSettings(Settings settings) {
        return settingsMapper.insertDefaultSettings(settings) > 0;
    }

    @Override
    public String getStrangerQuestionsById(Long uid) {
        return settingsMapper.selectQuestionById(uid);
    }

    /**
     * 查询用户问题
     *
     * @param userId
     * @return
     */
    @Override
    public String selectQuestionById(Long userId) {
        LambdaQueryWrapper<Settings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Settings::getUserId, userId);

        Settings settings = settingsMapper.selectOne(queryWrapper);
        if (settings == null) {
            return "";
        }
        return settings.getStrangerQuestion();
    }
}
